﻿using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography;
using BizModels.GuestBook;
using DataModel;
using IRepositories;
using System;
using System.Collections.Generic;

namespace Repositories
{
    public class GuestBookRepository : RepositoryBase, IGuestBookRepository
    {
        public GuestBookRepository()
        {
            Context = new HinokyEntities();
        }

        public List<GuestBookListT> GetGuestBookList(string locationCode, int subKey, int firstIdx, int pageSize, bool isAdmin = false)
        {
            #region 방명록 본문만 가져오기

            var query = from s in Context.GuestBook
                        where s.LocationCode == locationCode && s.SubKey == subKey && s.IsDelete == false && s.ParentIdx == null
                        orderby s.Idx descending
                        select new GuestBookListT
                        {
                            LocationCode = s.LocationCode,
                            SubKey = s.SubKey,
                            Idx = s.Idx,
                            ParentIdx = s.Idx,
                            Comment = s.Comment,
                            GuestName = s.GuestName,
                            InsertDate = s.InsertDate,
                            IsPrivate = s.IsPrivate,
                            IsDelete = s.IsDelete,
                            IPAddress = s.IPAddress,
                            SecureEmailAddress = s.SecureEmailAddress
                        };

            if (firstIdx != -1)
            {
                query = query.Where(s => s.Idx < firstIdx);
            }

            if (!isAdmin)
            {
                query = query.Where(s => s.IsPrivate == false);
            }

            var dataList = query.Take(pageSize).ToList();

            //var masterList = dataList.Where(s => s.ParentIdx == null).ToList();

            #endregion

            var parantKeyList = dataList.Select(s => s.Idx).ToList();

            #region 댓글의 답글 리스트 가져오기

            var replyDataList =
                Context.GuestBook.Where(s => s.ParentIdx != null && parantKeyList.Contains((int) s.ParentIdx))
                    .OrderBy(s => s.Idx)
                    .Select(s => new GuestBookListT
                    {
                        LocationCode = s.LocationCode,
                        SubKey = s.SubKey,
                        Idx = s.Idx,
                        ParentIdx = s.ParentIdx,
                        Comment = s.Comment,
                        GuestName = s.GuestName,
                        InsertDate = s.InsertDate,
                        IsPrivate = s.IsPrivate,
                        IsDelete = s.IsDelete,
                        IPAddress = s.IPAddress,
                        SecureEmailAddress = s.SecureEmailAddress
                    }).ToList();
            #endregion

            foreach (var master in dataList)
            {
                master.ReplyList = replyDataList.Where(s => s.ParentIdx == master.Idx).OrderBy(s => s.Idx).ToList();
            }

            return dataList;
        }

        public int AddGuestBook(GuestBookInsT guestBookInsT)
        {
            var data = new GuestBook
            {
                LocationCode = guestBookInsT.LocationCode,
                SubKey = guestBookInsT.SubKey,
                ParentIdx = guestBookInsT.ParentIdx,
                Comment = guestBookInsT.Comment,
                GuestName = guestBookInsT.GuestName,
                InsertDate = DateTime.Now,
                IsPrivate = guestBookInsT.IsPrivate,
                IsDelete = false,
                IPAddress = guestBookInsT.IPAddress,
                SecureEmailAddress = guestBookInsT.SecureEmailAddress
            };

            Context.GuestBook.Add(data);
            Context.SaveChanges();

            return data.Idx;
        }

        public GuestBookObjT GetGuestBook(string locationCode, int subKey, int idx)
        {
            var data = (from s in Context.GuestBook
                        where s.Idx == idx
                        select new GuestBookObjT()
                        {
                            LocationCode = s.LocationCode,
                            SubKey = s.SubKey,
                            Idx = s.Idx,
                            ParentIdx = s.ParentIdx,
                            Comment = s.Comment,
                            GuestName = s.GuestName,
                            InsertDate = s.InsertDate,
                            IsPrivate = s.IsPrivate,
                            IsDelete = s.IsDelete,
                            IPAddress = s.IPAddress,
                            SecureEmailAddress = s.SecureEmailAddress
                        }).SingleOrDefault();

            return data;
        }

        public int RemoveGuestBook(string locationCode, int subKey, int idx)
        {
            var data = Context.GuestBook.FirstOrDefault(s => s.LocationCode == locationCode && s.SubKey == subKey && s.Idx == idx);

            if (data != null) data.IsDelete = true;

            return Context.SaveChanges();
        }
    }

    /*
        public List<GuestBookListT> GetGuestBookList(int firstIdx, int pageSize, bool isAdmin = false)
        {
            var query = from s in Context.GuestBook where 
                s.ParentIdx == null && s.IsDelete == false
                orderby s.Idx descending select new GuestBookListT
                {
                    Comment = s.Comment,
                    Idx = s.Idx,
                    InsertDate = s.InsertDate,
                    IsMember = s.UserKey != 0,
                    IsPrivate = s.IsPrivate,
                    UserName = s.GuestName,
                    UserKey = s.UserKey ?? 0
                };

            if (firstIdx != 0)
            {
                query = query.Where(s => s.Idx < firstIdx);
            }

            if (!isAdmin)
            {
                query = query.Where(s => s.IsPrivate == false);
            }

            var resultList = query.Take(pageSize).ToList();

            // 회원명을 가지고 오기 위해 회원리스트를 불러온다. 
            var memberList = Context.Members.ToList();

            foreach (var result in resultList)
            {
                result.UserId = (from s in memberList where s.UserKey == result.UserKey select s.Id).FirstOrDefault();
            }

            return resultList;
        }

        public int AddGuestBook(GuestBookInsT guestBookInsT)
        {
            var data = new GuestBook
            {
                ParentIdx = guestBookInsT.ParentIdx,
                Comment = guestBookInsT.Comment,
                UserKey = guestBookInsT.UserKey,
                GuestName = guestBookInsT.GuestName,
                InsertDate = DateTime.Now,
                IsPrivate = guestBookInsT.IsPrivate,
                IsDelete = false,
                IPAddress = guestBookInsT.IpAddress,
                SecureEmailAddress = guestBookInsT.ContractText
            };

            Context.GuestBook.Add(data);
            Context.SaveChanges();

            return data.Idx;
        }


        public GuestBookListT GetGuestBook(int idx)
        {
            var data = (from s in Context.GuestBook
                where s.Idx == idx
                select new GuestBookListT
                {
                    Comment = s.Comment,
                    Idx = s.Idx,
                    InsertDate = s.InsertDate,
                    IsMember = s.UserKey != 0,
                    IsPrivate = s.IsPrivate,
                    UserName = s.GuestName,
                    UserKey = s.UserKey ?? 0
                }).SingleOrDefault();

            if (data.UserKey > 0)
                data.UserId = (from s in Context.Members where s.UserKey == data.UserKey select s.Name).FirstOrDefault();

            return data;
        }

        public List<GuestBookListT> GetGuestBookReplyList(int parentIdx, bool isLoadAll)
        {
            var dataList = new List<GuestBookListT>();

            var query = Context.GuestBook.OrderByDescending(s => s.Idx).Where(s => s.IsDelete == false && s.ParentIdx != null).Select(s => new GuestBookListT
            {
                //ParentIdx = s.ParentIdx == null ? 0 : (int)s.ParentIdx,
                ParentIdx = s.ParentIdx ?? 0,
                Comment = s.Comment,
                Idx = s.Idx,
                InsertDate = s.InsertDate,
                IsMember = s.UserKey != 0,
                IsPrivate = s.IsPrivate,
                UserName = s.GuestName,
                UserKey = s.UserKey ?? 0
            });

            if (!isLoadAll)
            {
                dataList.Where(s => s.ParentIdx == parentIdx).ToList();
            }
            else
            {
                dataList = query.ToList();
            }

            return dataList;
        }

        public int RemoveGuestBook(int idx)
        {
            var data = Context.GuestBook.FirstOrDefault(s => s.Idx == idx);

            if (data != null)
            {
                data.IsDelete = true;
            }

            return Context.SaveChanges();
        }
    }
         */


}
